import { Root, searchValueInterface } from '@/types'
import {
  delData,
  getBrand,
  getBrandSearch,
  getCommodityList,
  getCommodityType,
  getCustomList,
  getOrderList,
  getOrders,
  getRole,
  getcommodityClassify,
  getcommodityClassifySearch,
  historyadd,
  ordercancel,
  orderdateil,
  orderupdate,
  putBrandId,
  reDel,
  returns,
  returnsdetaile,
  rts,
  updatePermissions,
  updatePermissionsUser,
  updateUser,
  getCommodityClassifyId,
  getCommodityAttributeId,
  getCommodityParameter
} from './request'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'

// 获取角色权限列表
export function usePermissionsList() {
  return useQuery({
    queryKey: ['permissions/getPermissions'],
    queryFn: () => getRole()
  })
}
// 分页获取品牌数据
export function useBrand(
  pageNum: number,
  pageSize: number,
  searchBrand: string
) {
  return useQuery({
    queryKey: ['brand', pageNum, pageSize, searchBrand],
    queryFn: () => getBrand(pageNum, pageSize, searchBrand)
  })
}

// 修改品牌数据
export function usePutBrandId(obj: object) {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: () => putBrandId(obj),
    onSuccess: () => {
      queryClient.invalidateQueries('brand')
    }
  })
}

// 分页获取商品类型
export function useCommodityType(pageNum: number, pageSize: number) {
  return useQuery({
    queryKey: ['commodityType', pageNum, pageSize],
    queryFn: () => getCommodityType(pageNum, pageSize)
  })
}
// 订单详情获取
export function useOrderdateil(id: string) {
  return useQuery({
    queryKey: ['orderdateil', id],
    queryFn: () => orderdateil(id)
  })
}
// 分页获取商品分类
export function useCommodityClassify(pageNum: number, pageSize: number) {
  return useQuery({
    queryKey: ['commodityClassify', pageNum, pageSize],
    queryFn: () => getcommodityClassify(pageNum, pageSize)
  })
}

// 分页获取商品的分类检索
export function useCommodityClassifySearch() {
  return useQuery({
    queryKey: ['commodityClassifySearch'],
    queryFn: () => getcommodityClassifySearch()
  })
}

// 商品品牌检索
export function useBrandSearch() {
  return useQuery({
    queryKey: ['commodityBrandSearch'],
    queryFn: () => getBrandSearch()
  })
}

// 商品分类检索
export function useCommodityClassifySearchId(id: number) {
  return useQuery({
    queryKey: ['getCommodityClassifyId', id],
    queryFn: () => getCommodityClassifyId(id)
  })
}

// 商品属性检索
export function useCommodityAttributeId(id: number) {
  return useQuery({
    queryKey: ['getCommodityAttributeId', id],
    queryFn: () => getCommodityAttributeId(id)
  })
}

// 商品参数检索
export function useCommodityParameter(id: number) {
  return useQuery({
    queryKey: ['getCommodityAttributeId', id],
    queryFn: () => getCommodityParameter(id)
  })
}

// 分页获取商品列表数据
export function useCommodityList(
  pageNum: number,
  pageSize: number,
  searchValue: searchValueInterface
) {
  return useQuery({
    queryKey: ['commodityList', pageNum, pageSize, searchValue],
    queryFn: () => getCommodityList(pageNum, pageSize, searchValue)
  })
}

// 删除表数据方法
export function useDel(path: string) {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: (id: string) => delData(id, path),
    onSuccess: () => {
      queryClient.invalidateQueries('brand')
    }
  })
}
// 修改用户状态
export function useUpStatus(id: string, status: boolean) {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: () => updateUser(id, status),
    onSuccess: () => {
      queryClient.invalidateQueries('permissions/delUser')
    }
  })
}

//陈浩-订单获取
export function useOrderlist(
  event: number | undefined,
  status: number | undefined,
  SubmitTime: string | undefined,
  sourceType: number | undefined,
  orderType: number | undefined,
  number: string | undefined,
  receiverName: string | undefined
) {
  return useQuery({
    queryKey: [
      'order/orderlist',
      event,
      status,
      SubmitTime,
      sourceType,
      orderType,
      number,
      receiverName
    ],
    queryFn: () =>
      getOrderList(
        event,
        status,
        SubmitTime,
        sourceType,
        orderType,
        number,
        receiverName
      ),
    keepPreviousData: true
  })
}
//陈浩-订单状态的全部修改
export function useAllStatus(obj: object) {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: () => orderupdate(obj),
    onSuccess: () => {
      queryClient.invalidateQueries('order/orderlist')
    }
  })
}
//陈浩订单取消
export function useOrdercancel(id: string | undefined) {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: () => ordercancel(id),
    onSuccess: () => {
      queryClient.invalidateQueries('orderdateil')
    }
  })
}
//陈浩-备注
export function useHistoryadd(obj: object) {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: () => historyadd(obj),
    onSuccess: () => {
      queryClient.invalidateQueries('orderdateil')
    }
  })
}
//表格
export function useReturns(page: number, pageSize: number, obj: object) {
  return useQuery({
    queryKey: ['returns', page, pageSize, obj],
    queryFn: () => returns(page, pageSize, obj),
    keepPreviousData: true
  })
}
//删除
export function useRedel(list: any) {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: () => reDel(list),
    onSuccess: () => {
      queryClient.invalidateQueries('returns')
    }
  })
}
//returnsdetaile详情
export function useDetaile(id: string | null | undefined) {
  return useQuery({
    queryKey: ['red', id],
    queryFn: () => returnsdetaile(id)
  })
}
//状态
export function useRts(id: string | undefined, status: number | undefined) {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: () => rts(id, status),
    onSuccess: () => {
      queryClient.invalidateQueries('red')
    }
  })
}
export function usePermissions(id: string, PermissionsIds: string[]) {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: () => updatePermissions(id, PermissionsIds),
    onSuccess: () => {
      queryClient.invalidateQueries('permissions/delUser')
    }
  })
}

// 获取买家列表
export function useComstList(pageNum: number, pageSize: number) {
  return useQuery({
    queryKey: ['user/custom-list', pageNum, pageSize],
    queryFn: () => getCustomList(pageNum, pageSize)
  })
}
// 首页图表数据获取
export function useOrders() {
  return useQuery({
    queryKey: ['order/orders'],
    queryFn: () => getOrders()
  })
}

// 修改用户权限
export function useUpdatePermissions(MenuList: Root) {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: (id: string) => updatePermissionsUser(id, MenuList),
    onSuccess: () => {
      queryClient.invalidateQueries('permissions/getPermissions')
    }
  })
}
